package com.example.shopping.controller;

import com.example.shopping.entity.GoodsType;
import com.example.shopping.entity.Province;
import com.example.shopping.service.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/province")
public class ProvinceController {
    @Autowired
    private ProvinceService provinceService;

    // 显示省份列表
    @GetMapping("/list")
    public String listProvinces(Model model) {
        model.addAttribute("provinces", provinceService.findAll());
        return "provinceList";
    }


    // 显示修改页面
    @GetMapping("/edit")
    public String edit(@RequestParam String pid, Model model) {


        Province province =provinceService.findByPid(pid);
        model.addAttribute("province", province);
        return "provinceEdit"; // 返回 goodsTypeEdit.html 页面
    }

    // 提交更新请求（稍后可以加）
    @PostMapping("/update")
    public String updateProvince(@ModelAttribute Province province) {
        provinceService.updateProvince(province);
        return "redirect:/province/list";
    }

    @GetMapping("/delete")
    public String deleteProvince(@RequestParam("pid") String pid) {
        provinceService.deleteByPid(pid); // 调用删除方法

        return "redirect:/province/list"; // 删除完成后重定向到用户列表页
    }


    @GetMapping("/add")
    public String add() {
        return "provinceAdd";
    }

    @PostMapping("/save")
    public String saveGoodsType( Province province) {


        provinceService.saveProvince(province); // 调用服务层保存用户

        return "redirect:/province/list";
    }
}
